using System;
using System.Collections.Generic;
using System.Text;

using System.Threading;

namespace Bots.Index
{
    public enum ObjectOperateType{
        Add,
        Delete,
        Update
    }

    internal class ShareBufferPool
    {
        //Buffer for new object
        private static Queue<DomainObject> addBuffer;
        //Buffer for delete object
        private static Queue<DomainObject> deleteBuffer;
        //Buffer for update object
        private static Queue<DomainObject> updateBuffer;
        //Buffer for produce object
        private static SharedBuffer<DomainObject> produceBuffer;

        static public Queue<DomainObject> AddBuffer
        {
            get { return addBuffer; }
        }

        static public Queue<DomainObject> UpdateBuffer
        {
            get { return updateBuffer; }
        }

        static public Queue<DomainObject> DeleteBuffer
        {
            get { return deleteBuffer; }
        }

        static ShareBufferPool() {
            addBuffer = new Queue<DomainObject>(10);
            deleteBuffer = new Queue<DomainObject>(10);
            updateBuffer = new Queue<DomainObject>(10);
            produceBuffer = new SharedBuffer<DomainObject>(200);
        }

        static public void Produce(DomainObject obj) {
            produceBuffer.Produce(obj);
        }

        static public void Block()
        {
            produceBuffer.Block();
            Dispatch();
        }

        static private void Dispatch()
        {
            for (int i = 0; i < produceBuffer.Length; i++) {
                DomainObject obj = produceBuffer.Consume();
                if (obj.ObjectStatue == ObjectStatue.New)
                {
                    addBuffer.Enqueue(obj);
                }
                else if (obj.ObjectStatue == ObjectStatue.Delete)
                {
                    deleteBuffer.Enqueue(obj);
                }
                else if (obj.ObjectStatue == ObjectStatue.Update)
                {
                    updateBuffer.Enqueue(obj);
                }
            }
        }

      
    }
}
